#include <51func.h>
void A(){
    printf("before A\n");
    sleep(3);
    printf("after A\n");
}
void B(){
    printf("before B\n");
    sleep(3);
    printf("after B\n");
}
typedef struct shareRes_s {
    int flag; //条件 0 A没做完 1 A做完了
    pthread_mutex_t mutex;// 锁保护条件资源
} shareRes_t;
void * threadFunc(void *arg){
    shareRes_t * pshareRes = (shareRes_t *)arg;
    //sleep(1);
    // t2
    while(1){
        pthread_mutex_lock(&pshareRes->mutex);
        if(pshareRes->flag != 0){
            pthread_mutex_unlock(&pshareRes->mutex);
            break;
        }
        pthread_mutex_unlock(&pshareRes->mutex);
    }
    B();
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    shareRes_t shareRes;
    shareRes.flag = 0;
    pthread_mutex_init(&shareRes.mutex,NULL);
    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,&shareRes);
    sleep(1);
    // t1
    A();
    pthread_mutex_lock(&shareRes.mutex);
    shareRes.flag  = 1;
    pthread_mutex_unlock(&shareRes.mutex);

    pthread_join(tid,NULL);
    return 0;
}

